<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">



        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请人</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowuser" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请部门</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowdept" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>用证项目</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="choose_engin()">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="choose_engin()">
                                <i class="layui-icon layui-icon-search"></i>
                            </span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label" id="dt_title">报建日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="dt" id="dt" placeholder="请输入" clearable autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea" id="explain_label">用证说明</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">关联单据</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="flowrelas" class="layui-input flowrelas" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');">
                            <div class="choose_div" id="flowrelas" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="advanersDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">预审人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="advaners"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="nexterDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>指定下一步审核人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="nexter"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>用证明细</span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_choose" onclick="check_cert()">从证书库选择</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th style="width: 45px;">序号</th>
                            <th><text>*</text>岗位</th>
                            <th><text>*</text>证书名称</th>
                            <th>证书编号</th>
                            <th>证书专业</th>
                            <th>持证人</th>
                            <th>使用情况</th>
                            <th>所属公司</th>
                            <th>是否锁证</th>
                            <th>使用费(元/月)</th>
                            <th>使用开始时间</th>
                            <th>预计结束时间</th>
                            <th>使用结束时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    var param_isturn = getQueryVariable('isturn') || 0;
    if(param_isturn > 0){
        $("#billzhan").remove();
        $("#billtutn").remove();
        $("#billsub").attr('isshen', 1);
    }
    var param_billyushen = getQueryVariable('billyushen') || 0;
    if(param_billyushen == 2){
        $("#billyushen").addClass('layui-hide');
    }

    function check_cert() {
        openPage('../public/choose_cert.php?page=cert_use&choose_type=checkbox','80%','80%');
    }

    function check_cert_one(obj){
        var index = $(obj).parents('tr').index();
        openPage('../public/choose_cert.php?page=cert_use_one&index='+index,'80%','80%');
    }

    // 选择项目
    function choose_engin() {
        openPage('../public/choose_engin.php?page=cert_use&dom=enginid','80%','80%')
    }


    var placeArr;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_flowdata_edit_cert_use, data:{id: param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                $("#flowsetname").text(data.flowsetname);
                $("#flowuser").val(data.flowuser);
                $("#flowdept").val(data.flowdept);
                if(data.isadvan){
                    $("#advanersDiv").removeClass('layui-hide');
                    if(data.advaners_info.length > 0){
                        tan_tags_init('#advaners', data.advaners_info);
                    }
                }
                if(data.isnext){
                    $("#nexterDiv").removeClass('layui-hide');
                    if(data.nexter_info.length > 0){
                        tan_tags_init('#nexter', data.nexter_info);
                    }
                }
                if(data.flowrelas_info.length > 0){
                    set_choose_btn_more('flowrelas',data.flowrelas_info);
                }



                $("#dt").val(data.dt);
                $("#explain").val(data.explain);


                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                }



                placeArr = res.data.place;

                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><select class="layui-select place">';
                        for (var mmm in placeArr) {
                            if(list[key].place == placeArr[mmm]){
                                str += '<option value="'+placeArr[mmm]+'" selected>'+placeArr[mmm]+'</option>';
                            }else{
                                str += '<option value="'+placeArr[mmm]+'">'+placeArr[mmm]+'</option>';
                            }
                        }
                        str += '</select></td>';
                        str += '<td><input type="text" onclick="check_cert_one(this)" certid="'+list[key].certid+'" readonly value="'+list[key].name+'" class="layui-input disabled name"></td>';
                        str += '<td><input type="text" class="layui-input numberss disabled" readonly value="'+list[key].number+'"></td>';
                        str += '<td><input type="text" class="layui-input major_name disabled" readonly value="'+list[key].major_name+'"></td>';
                        str += '<td><input type="text" class="layui-input person_name disabled" readonly value="'+list[key].person_name+'"></td>';
                        str += '<td><input type="text" class="layui-input statueuse_name disabled" readonly value="'+list[key].statueuse_name+'"></td>';
                        str += '<td><input type="text" class="layui-input chanid_name disabled" readonly value="'+list[key].chanid_name+'"></td>';

                        str += '<td><select class="layui-select issuo">';
                        if(list[key].issuo == '是'){
                            str += '<option value="是" selected>是</option>';
                            str += '<option value="否">否</option>';
                        }else{
                            str += '<option value="是">是</option>';
                            str += '<option value="否" selected>否</option>';
                        }
                        str += '</select></td>';
                        str += '<td><input type="number" class="layui-input usefee" value="'+list[key].usefee+'"></td>';
                        str += '<td><input type="text" class="layui-input startdt bdate_ymd" readonly value="'+list[key].startdt+'"></td>';
                        str += '<td><input type="text" class="layui-input yudt bdate_ymd"  readonly value="'+list[key].yudt+'"></td>';
                        str += '<td><input type="text" class="layui-input enddt bdate_ymd" readonly value="'+list[key].enddt+'"></td>';

                        str += '<td><select class="layui-select opt">';
                        if(list[key].opt > 0){
                            str += '<option value="1" selected>在用</option>';
                            str += '<option value="0">停用</option>';
                        }else{
                            str += '<option value="1">在用</option>';
                            str += '<option value="0" selected>停用</option>';
                        }
                        str += '</select></td>';
                        str += '</tr>';
                    }
                    $("#list_list").html(str);
                }
                update_refresh_row_func();

                public_upload_file_load('upload_files', res.data.files);
                render_date();
                render_from();
            }
        });



        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.isshen = $(this).attr('isshen') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            data.field.enginid = get_choose_val('enginid');

            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();

                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }
                if(!validRes){return false;}
            }


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var place = $(this).find('.place').val();
                    var certid = $(this).find('.name').attr('certid');
                    var issuo = $(this).find('.issuo').val();
                    var usefee = $(this).find('.usefee').val();
                    var startdt = $(this).find('.startdt').val();
                    var enddt = $(this).find('.enddt').val();
                    var yudt = $(this).find('.yudt').val();
                    var opt = $(this).find('.opt').val();
                    var index = i + 1;

                    if(data.field.isturn > 0){
                        if(!place){
                            throw new Error('第'+index+'行，请选择岗位');
                        }
                        if(!certid){
                            throw new Error('第'+index+'行，请选择证书名称');
                        }


                        var chongKey = check_chongfu(i, certid);
                        if(chongKey){
                            throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                        }
                    }


                    list.push({
                        place:place,
                        certid:certid,
                        issuo:issuo,
                        usefee:usefee,
                        startdt:startdt,
                        enddt:enddt,
                        yudt:yudt,
                        opt:opt,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.id = param_id
            data.field.files = public_upload_file_value('upload_files');
            var butsss = $(this).attr('but');


            if(butsss == 3){
                facade.ajax({url: ask_cert_use_check, data:data.field}).done(function(res){
                    if (res.code === 2000) {
                        var info = res.data;

                        if(info.status){
                            layer.confirm("<div class='tit'>证书使用情况提示：</div><div class='exp'>"+info.data+"</div>", {
                                    title:'操作确认',
                                    icon: 3,
                                    area:['600px','400px'],
                                    btn:['确定']
                                }, function(conindex){
                                    public_flow_submit(data.field, ask_flowdata_add_cert_use, ask_flowdata_edit_cert_use, butsss);
                                    layer.close(conindex);
                                }
                            )
                        }else{
                            public_flow_submit(data.field, ask_flowdata_add_cert_use, ask_flowdata_edit_cert_use, butsss);
                        }
                    }
                });
            }else{
                public_flow_submit(data.field, ask_flowdata_add_cert_use, ask_flowdata_edit_cert_use, butsss);
            }

            return false;
        });
    });





    //检测重复
    function check_chongfu(index, certid){
        try {
            $('#list_list tr').each(function (i) {
                var cur_certid = $(this).find('.name').attr('certid');
                if(index != i){
                    if(certid == cur_certid){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }
</script>
</body>
</html>
